package com.anycc.cloud.common.dto.query;

import lombok.Data;

/**
 * 排序类
 */
@Data
public class OrderBy {
    public static final String ASC = "asc";
    public static final String DESC = "desc";
    private String direction;
    private String property;

    public OrderBy() {
    }

    public OrderBy(String property) {
        this("asc", property);
    }

    public OrderBy(String direction, String property) {
        this.setDirection(direction);
        this.setProperty(property);
    }

    public String getDirection() {
        return this.direction;
    }

    public void setDirection(String direction) {
        if (this.isAsc(direction)) {
            this.direction = "asc";
        } else {
            if (!this.isDesc(direction)) {
                throw new IllegalArgumentException(String.format("Invalid value '%s' for order direction ! Has to be either 'desc' or 'asc' (case insensitive).", new Object[]{direction}));
            }

            this.direction = "desc";
        }

    }

    public boolean isAsc(String direction) {
        return direction != null && "asc".equalsIgnoreCase(direction);
    }

    public boolean isAsc() {
        return this.isAsc(this.direction);
    }

    public boolean isDesc(String direction) {
        return direction != null && "desc".equalsIgnoreCase(direction);
    }

    public boolean isDesc() {
        return this.isDesc(this.direction);
    }


}